package deprecated.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import util.DomainUtils;
import util.FormArgumentsException;
import deprecated.hibernate.HibernateUtil;
import domain.dao.interfaces.IDAOContactGroup;
import domain.entities.interfaces.IUser;

/**
 * Servlet implementation class AddContactGroupServlet
 */
public class AddContactGroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddContactGroupServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String groupname = DomainUtils.trim(request.getParameter("group_name"));
		String[] contactsOfGroup = request.getParameterValues("contacts_of_group");

		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {

			checks(request, groupname, contactsOfGroup);

			IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
			daoContactGroup.addContactGroup(
					
					((IUser)request.getSession(false).getAttribute(DomainUtils.CURRENT_USER_STRING)).getId(),
					groupname,
					contactsOfGroup);
//			DAOFactory.getDaoContactGroup().addContactGroup(
//					null,
//					((User)request.getSession(false).getAttribute(DomainUtils.CURRENT_USER_STRING)).getId(),
//					groupname,
//					contactsOfGroup);


			DomainUtils.displayInfoMessage(
					request, 
					DomainUtils.ADD_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
					"Contact group created successfully.");
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
				System.err.println(e);
			}

			DomainUtils.keepParametersContent(request);
		}


		request.getRequestDispatcher("pages/addContactGroup.jsp").forward(request, response);
	}

	private void checks(HttpServletRequest request, String groupName, String[] contactsOfGroup) 
			throws Exception {

		HttpSession session = request.getSession(false);


		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}


		//Checking group name
		if(groupName == null || groupName.isEmpty())
		{
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"The name of the group should'nt be empty"); 
			throw new FormArgumentsException();
		}

		if(DomainUtils.startByALetter(groupName) == false && 
				DomainUtils.startByUnderscore(groupName) == false) {

			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"The name of the group must start by a letter or an underscore character."); 
			throw new FormArgumentsException();
		}

		if(DomainUtils.containsALetter(groupName) == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"The name of the group must contains at least one letter.");
			throw new FormArgumentsException();
		}

		if(DomainUtils.match(groupName, "\\w+") == false){
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"The name of the group must contains only letter, digital or underscore, characters."); 
			throw new FormArgumentsException();
		}
	}
}
